'''

查找图片
    对比不同模型检测结果图片

输入：
测试集(labelme格式): D:\DATA\20250611HKBZ\caitu\testdata170
模型结果标注：D:\DATA\20250611HKBZ\temp\20250618141520_detr-r101-dc5_800_traindata580\annotated
模型结果图片：D:\DATA\20250611HKBZ\temp\20250618141520_detr-r101-dc5_800_traindata580\show_img
对比结果图片：D:\DATA\20250611HKBZ\temp\20250618141520_detr-r101-dc5_800_traindata580\val_show_img


'''
import glob
import json
import os
import sys
import traceback

# 输入
test_data_dir = r'D:\DATA\20250611HKBZ\caitu\before_20250619\testdata170'
# test_data_dir = r'D:\DATA\20250611HKBZ\trainV8Seg_allv2\add_imgs\lgy'
result_dirs = [r'D:\DATA\20250611HKBZ\temp\20250618141520_detr-r101-dc5_800_traindata580',
               r'D:\DATA\20250611HKBZ\temp\20250615164543_yolov8mSeg_416_allv24',
               ]
# result_dirs = []

# query_str = 'aircraft' # 类别名
query_str = 'thumbs_up'


# 查找文件名列表
target_file_inds = []
test_img_paths = glob.glob(f'{test_data_dir}/*.jpg')
test_json_paths = glob.glob(f'{test_data_dir}/*.json')
for json_path in test_json_paths:
    json_dict = json.load(open(json_path))
    for rect_shape_dict in json_dict['shapes']:
        cls_name = rect_shape_dict['label']
        if cls_name == query_str:
            json_filename = os.path.basename(json_path) # 000100.json
            file_ind = json_filename[:-5] # '000100'
            target_file_inds.append(file_ind) #

# 根据文件名获取目标图片
# n---- n个图片名*m个模型*2类别（show，val_show）
target_dict = {} # fileind: {modelname:{'show_img':_,'val_show_img':_}}

for file_ind in target_file_inds:
    target_dict[file_ind] = {}

    for result_dir in result_dirs:

        # model_annotated_save_dir = rf'{result_dir}\annotated'
        model_show_img_dir = rf'{result_dir}\show_img'
        model_val_show_img_dir = rf'{result_dir}\val_show_img'

        model_name = os.path.basename(result_dir)
        model_show_img_path = f'{model_show_img_dir}\{file_ind}.jpg'
        model_val_show_img_path = f'{model_val_show_img_dir}\{file_ind}.jpg'
        target_dict[file_ind][model_name] = {
            'show_img': model_show_img_path,
            'val_show_img': model_val_show_img_path,
        }

import pprint
pprint.pprint(target_dict)# 美观输出target_dict


# 显示targets
from show_img_pyqt import ImageWithResizableBox
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QComboBox

app = QApplication(sys.argv)
test_config = {
    'target_dict': target_dict,
    'origin_scale': 0.4,
}
try:
    widget = ImageWithResizableBox(test_config)  # 替换为初始图片路径
    widget.resize(widget.current_pixmap.width(), widget.current_pixmap.height())  # 调整窗口大小以适应图片
    widget.show()
    sys.exit(app.exec_())
except:
    print(traceback.format_exc())

